/**
 * TO BE DETERMINED
 */
package org.eichar.client.question;

import java.util.ArrayList;
import java.util.Collection;

import org.eichar.client.I18N;
import org.eichar.client.LanguageConstants;
import org.eichar.client.Question;
import org.eichar.client.QuestionPanel;
import org.eichar.client.WidgetFactory;
import org.eichar.client.observer.question.AnswerTextObserver;
import org.eichar.client.shared.Word;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * A questions that asks for the translation of a word or phrase.
 * 
 * @author Jesse
 */
public class TranslationQuestion extends Question {

    private final Word question;

    private final Word[] answers;

    private final I18N i18n;

    private final WidgetFactory widgetFactory;

    private QuestionPanel observer;

    private AnswerTextObserver answerTextObserver;

    /**
     * Create question. Both strings should be in UTF-8
     * 
     * @param question
     *            question word/phrase
     * @param answers
     *            possible answers
     * @param constants
     * @param messages
     */
    public TranslationQuestion(final Word question, final Word[] answers,
            I18N i18n, WidgetFactory widgetFactory) {
        this.widgetFactory = widgetFactory;
        this.question = question;
        if (answers == null) {
            this.answers = new Word[0];
        } else {
            this.answers = new Word[answers.length];
            for (int i = 0; i < answers.length; i++) {
                this.answers[i] = answers[i];
            }
        }
        this.i18n = i18n;

        createPanelController();
    }

    private void createPanelController() {
        LanguageConstants constants = i18n.getConstants();

        VerticalPanel panel = widgetFactory.createVerticalPanel();
        panel.setSize("100%", "100%"); //$NON-NLS-1$//$NON-NLS-2$

        Label directions = widgetFactory.createLabel(constants
                .translationQuestionDirections(), true);
        directions.setWidth("100%"); //$NON-NLS-1$
        panel.add(directions);

        HorizontalPanel hPanel = widgetFactory.createHorizontalPanel();
        hPanel.setWidth("100%"); //$NON-NLS-1$

        HTML toTranslate = widgetFactory
                .createHTML("<b>" + question.getWord() + "</b>"); //$NON-NLS-1$ //$NON-NLS-2$
        hPanel.add(toTranslate);

        TextBox answerText = widgetFactory.createTextBox();
        answerText.setText(constants.enterAnswer());
        answerText.setWidth("100%"); //$NON-NLS-1$
        hPanel.add(answerText);

        panel.add(hPanel);

        // TODO define observers
        answerTextObserver = new AnswerTextObserver(answerText);
        Collection/* <Observer> */observers = new ArrayList/* <Observer> */();

        observer = new QuestionPanel(panel, observers);
    }

    /**
     * 
     * @see org.eichar.client.Question#getPanel()
     */
    /* @Override ANNOTATION */
    public QuestionPanel getPanel() {
        return observer;
    }

    /**
     * @see org.eichar.client.Question#isCorrect()
     */
    // @Override ANNOTATION
    public boolean isCorrect() {
        // TODO Auto-generated method stub
        return false;
    }

    /**
     * @see org.eichar.client.Question#setFocus(boolean)
     */
    public void setFocus(boolean focused) {
        ((TextBox) answerTextObserver.getWidget()).setFocus(focused);
    }

}
